package com.serkol.abmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Tracks;
import com.serkol.abmanager.entity.Users;
import com.serkol.abmanager.service.UsersService;
import com.serkol.abmanager.mapper.UsersMapper;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {
    @Override
    public GridResult findByList(int page, int pagesize, String name, long rfid, long deptId, long groupId, int groupType, int roleId) {

        GridResult gridResult = new GridResult();
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Users::getId);

        if (!name.isEmpty()) {
            queryWrapper.lambda().likeRight(Users::getName, name);
        }
        if (rfid > 0) {
            queryWrapper.lambda().eq(Users::getRfid, rfid);
        }
        if (deptId > 0) {
            queryWrapper.lambda().eq(Users::getDeptId, deptId);
        }
        if (groupId > 0) {
            queryWrapper.lambda().eq(Users::getGroupId, groupId);
        }
        if (groupType > 0) {
            queryWrapper.lambda().eq(Users::getGroupType, groupType);
        }
        if (roleId == -1) {
            queryWrapper.lambda().ne(Users::getRoleId, 0);
            // queryWrapper.lambda().eq(Users::getRoleId,roleId);
        } else {
            if (roleId > 0) {
                queryWrapper.lambda().eq(Users::getRoleId, roleId);
            }
        }
        Page<Users> page0 = new Page<>(page, pagesize);

        //   List<Map<String, Object>> maps = this.baseMapper.selectPageList(page0, queryWrapper);

        IPage<Map<String, Object>> page1 = this.baseMapper.selectPageList(page0, queryWrapper);
        if (page1.searchCount()) {
            long count = this.count(queryWrapper);
            gridResult.setPage(page1.getCurrent());
            gridResult.setTotal(count);
            int pagesize1 = Long.valueOf(count).intValue() / pagesize;
            if (pagesize1 <= 0) {
                pagesize1 = 1;
            }
            gridResult.setPages(pagesize1);
            gridResult.setData(page1.getRecords());
            gridResult.setStatus(ResultConstant.SUCSESS);
        } else {
            gridResult.setStatus(ResultConstant.EMPTIY);
        }

        return gridResult;
    }


}




